package com.wx.web.controller.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.eachonline.core.JsonResult;
import com.eachonline.util.RedisHelper;
import com.wx.business.util.MsgType;
import com.wx.business.util.SmsUtil;


@Controller
@RequestMapping("/common")
public class ComonToolsController  {
	
	 
	
	 @RequestMapping(value = "/sendCode", method = { RequestMethod.POST })
	 @ResponseBody
	 public JsonResult useRegist(HttpServletRequest  request ,HttpServletResponse response,int type,String tel) throws Exception {
		response.setHeader("Access-Control-Allow-Origin", "*");
		String code=RandomStringUtils.randomNumeric(4);
		String result="";
		if(StringUtils.isNotEmpty(type+""))
		{
			result=SmsUtil.sendMsgToUser(tel, MsgType.getValue(type), code);
		}
		
		
		if("0".equalsIgnoreCase(result))
		{
			String key=type+":"+tel+":"+code;
			RedisHelper.setex(key, code, 60*3);
			return new JsonResult(true);
		}
		
		return new JsonResult(false, "短信发送失败，错误代码：【"+result+"】");
        
	}

	
	 

	

}
